// pages/coupon/index.js

import {
  getNavigationbar
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
      couponDetail: {},
      brandList: [],
      catList: [],
      petTypeList: [],
      scrollViewHeight: 0,
      currentPage: 1,
      goodsList: [],
      isSpecs: false,
      p: 1,
      id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      if (options.data) {
        const params = JSON.parse(options.data)
        this.data.id = params.id
        getNavigationbar({
          act: 'coupon_pro_list',
          couponid: params.id,
          p: this.data.p
        }).then(res => { 
          console.log(res)
          res.coupon_info.starttimeStr = this.formatDate(res.coupon_info.starttime)
          res.coupon_info.exptimeStr = this.formatDate(res.coupon_info.exptime)
          if (res.coupon_info.exptime > Date.parse(new Date()) / 1000) {
            res.coupon_info.overdue = true;
          }
          console.log('res.pro_list', res.pro_list)
          this.setData({ 
            couponDetail: res.coupon_info,
            goodsList: res.pro_list
          });
          console.log(this.data.goodsList)
        })
      }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.computeScrollViewHeight()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    formatDate(timestamp) {
      // 将时间戳转换为Date对象
      var date = new Date(timestamp * 1000);
      // 获取年月日时分秒
      var year = date.getFullYear();
      var month = date.getMonth() + 1; // 月份是从0开始的
      var day = date.getDate();
      // 为单个数字前面补零
      month = month < 10 ? '0' + month : month;
      day = day < 10 ? '0' + day : day;
      // 组合成日期格式
      return year + '-' + month + '-' + day ;
    }, 

    handleScrollToLower() {
      console.log('触发到底部提示打印');
      this.data.p++
      getNavigationbar({
        act: 'coupon_pro_list',
        couponid: this.data.id,
        p: this.data.p
      }).then(res => { 
        this.data.goodsList = this.data.goodsList.concat(res.pro_list)
        console.log('res.pro_list', res.pro_list)
        this.setData({ 
          goodsList: this.data.goodsList
        });
        console.log(this.data.goodsList)
      })
    },

    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
      let that = this;
      let query = wx.createSelectorQuery().in(this);
      query.select('.header').boundingClientRect();
      query.exec(res => {
          let headerHeight = res[0].height;
          wx.getSystemInfo({
              success: (res) => {
                  let winHeight = res.windowHeight;
                  let scrollHeight = winHeight - headerHeight; // 滚动区域高度
                  that.setData({
                      scrollViewHeight: scrollHeight,
                  });
              }
          });
      });
  },
    /**
     * 选择商品规格加入购物车回调函数方法
     */
    handleJoinCart(e) {
      console.log('e', e)
      let data = {
          ...e.currentTarget.dataset.value
      }
      //详细接口
      getNavigationbar({
          act: 'good_detail',
          id : data.id
      }).then(res => {
          data = {...res,...e.currentTarget.dataset.value}
          console.log('详细接口', data)
          this.setData({
              isSpecs: true,
              objData: data
          })
          console.log(this.data.objData)
      })
    
  },
    /**
     * 完成规格选择将商品添加至购物车
     */
    handleConfirm(e) {
      let data = e.detail

      console.log("data", data)
      // if (data.type == 1) {
      getNavigationbar({
        act: 'cart',
        id: data.commodityId || '',
        propertyid: data.data.propertyid || '',
        numbers: data.count || '',
      }).then(res => {
      console.log('添加购物车', res)
      if (res.result == 101) {
        wx.nextTick(() => {
          getApp().Tips({
            title: res.msg
          })
        })
      } else {
        this.setData({
          isSpecs: false,
          cartGoodDetail: {}
        })
        wx.nextTick(() => {
          getApp().Tips({
            title: "商品已添加至购物车"
          })
        })
      }
    });

  },







})